<template>
	<section class="ruleManage">
		<el-row>
			<el-col :span="8" >
				<h4>数据规则列表</h4>
				<el-row class="header">
					<el-col :span="24">
						<el-button type="primary" icon="plus" size="small" @click="addRule()">新增</el-button>
						<el-button type="success" icon="edit" size="small" @click="editRule()">修改</el-button>
						<el-button type="danger" icon="edit" size="small" @click="delRule()">删除</el-button>
					</el-col>
				</el-row>
				<el-table :data="dataruletable.tableData" v-loading="dataruletable.tableLoading" border highlight-current-row style="width: 100%" @selection-change="handleRuleSelectChange" @current-change="handleRuleRowChange">
					<el-table-column type="selection" width="30">
                    </el-table-column>
                    <el-table-column prop="moduleName" label="模块名称" width="180">
					</el-table-column>
					
					<el-table-column prop="ruleName" label="名称" width="180">
					</el-table-column>
					<el-table-column prop="ruleCode" label="代码" width="180">
					</el-table-column>
					
				</el-table>
				<el-pagination @size-change="handleSizeRuleChange" @current-change="handleCurrentRuleChange" :current-page.sync="dataruletable.page" :page-sizes="[10, 20, 30, 40]" :page-size="dataruletable.rows" layout="sizes, prev, pager, next" :total="dataruletable.total">
				</el-pagination>
			</el-col>
			<el-col :span="8">
				<h4>规则分组</h4>
				<el-row class="header">
					<el-col :span="24">
						<el-button type="primary" icon="plus" size="small" @click="addGroup()">新增</el-button>
						<el-button type="success" icon="edit" size="small" @click="editGroup()">修改</el-button>
						<el-button type="danger" icon="edit" size="small" @click="delGroup()">删除</el-button>
					</el-col>
				</el-row>
				<el-table :data="dataruleGrouptable.tableData" v-loading="dataruleGrouptable.tableLoading" border highlight-current-row style="width: 100%" @current-change="handleGroupRowChange">
								<el-table-column prop="operate" label="操作" width="180">
								</el-table-column>
								<el-table-column prop="groupName" label="分组名称" width="180">
								</el-table-column>
							</el-table>
							<el-pagination @size-change="handleSizeGroupChange" @current-change="handleCurrentGroupChange" :current-page.sync="dataruleGrouptable.page" :page-sizes="[10, 20, 30, 40]" :page-size="dataruleGrouptable.rows" layout="sizes, prev, pager, next" :total="dataruleGrouptable.total">
							</el-pagination>

			</el-col>
			<el-col :span='8' >
				<h4>规则条件列表</h4>
				<el-row class="header">
					<el-col :span="24">
						<el-button type="primary" icon="plus" size="small" @click="addCondition()">新增</el-button>
						<el-button type="success" icon="edit" size="small" @click="editCondition()">修改</el-button>
						<el-button type="danger" icon="edit" size="small" @click="delCondition()">删除</el-button>
					</el-col>
				</el-row>
					<el-table :data="dataruleConditiontable.tableData" v-loading="dataruleConditiontable.tableLoading" border highlight-current-row style="width: 100%" @current-change="handleConRowChange">
						<el-table-column prop="fieldName" label="字段" width="180">
						</el-table-column>
						<el-table-column prop="conOperate" label="操作" width="180">
						</el-table-column>
						<el-table-column prop="fieldValue" label="字段值" width="180">
						</el-table-column>
						<el-table-column prop="conOrder" label="条件排序" width="180">
						</el-table-column>
						<el-table-column prop="fieldType" label="字段类型" width="180">
						</el-table-column>
					</el-table>
					<el-pagination @size-change="handleSizeConditionChange" @current-change="handleCurrentConditionChange" :current-page.sync="dataruleConditiontable.page" :page-sizes="[10, 20, 30, 40]" :page-size="dataruleConditiontable.rows" layout="sizes, prev, pager, next" :total="dataruleConditiontable.total">
					</el-pagination>
			</el-col>

			<!-- 弹窗   v-for="(operationcodes,index) in operationcodes" :key="index"-->
			<el-dialog title="数据规则" v-model="dataruleShow">
				<el-form :inline="true" :model="datarule" label-width="90px" class="submitDialog">
					<el-form-item label="模块名称" prop="moduleName">
						<el-input v-model="datarule.moduleName" placeholder="模块名称"></el-input>
					</el-form-item>
					<el-form-item label="规则代码" prop="ruleCode">
						<el-input v-model="datarule.ruleCode" placeholder="规则代码"></el-input>
					</el-form-item>
					<el-form-item label="规则名称" prop="ruleName">
						<el-input v-model="datarule.ruleName" placeholder="规则名称"></el-input>
					</el-form-item>
					<el-form-item label="状态" prop="status">
						<el-select v-model="datarule.status" placeholder="请选择状态">
							<el-option
						      v-for="item in statusOptions"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						      </el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="sql" prop="sql">

						<el-tag type="success">{{datarule.sql}}</el-tag>
					</el-form-item>
					<el-form-item style="text-align: right;width: 100%;">
						<el-button type="primary" @click.native="saveRule()">确定</el-button>
						<el-button @click.native="dataruleShow=false">取消</el-button>
					</el-form-item>
				</el-form>
			</el-dialog>

			<el-dialog title="数据规则分组" v-model="dataruleGroupShow">
				<el-form :inline="true" :model="dataruleGroup" label-width="90px" class="submitDialog" >
					<el-form-item label="操作" prop="operate">
						<el-select v-model="dataruleGroup.operate" placeholder="操作">
							<el-option
						      v-for="item in conOp"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						    </el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="分组名称" prop="groupName">
						<el-input v-model="dataruleGroup.groupName" placeholder="分组名称"></el-input>
					</el-form-item>
					<el-form-item label="上级分组" prop="pgroupid">
						<el-select v-model="dataruleGroup.pgroupid" clearable placeholder="上级分组">
							<el-option v-for="n in dataruleGrouptable.groupList" :key="n.id" :label="n.groupName" :value="n.id"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="状态" prop="status">
						<el-select v-model="dataruleGroup.status" placeholder="请选择状态">
							<el-option
						      v-for="item in statusOptions"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						      </el-option>
						</el-select>
					</el-form-item>

					<el-form-item style="text-align: right;width: 100%;">
						<el-button type="primary" @click.native="saveGroup()">确定</el-button>
						<el-button @click.native="dataruleGroupShow=false">取消</el-button>
					</el-form-item>
				</el-form>
			</el-dialog>

			<el-dialog title="数据规则条件" v-model="dataruleConditionShow">
				<el-form :inline="true" :model="dataruleCondition" label-width="90px" class="submitDialog">
					<el-form-item label="操作" prop="conditionoperate">
						<el-select v-model="dataruleCondition.operate" placeholder="请选择操作">
							<el-option
						      v-for="item in conOp"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						    </el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="字段名称" prop="fieldName">
						<el-input v-model="dataruleCondition.fieldName" placeholder="字段名称"></el-input>
					</el-form-item>
					<el-form-item label="操作条件" prop="conOperate">
						<el-select
							 v-model="dataruleCondition.conOperate" placeholder="请选择操作条件">
							<el-option
						      v-for="item in sqlOp"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						      </el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="字段类型" prop="fieldType">

						<el-select v-model="dataruleCondition.fieldType" clearable placeholder="请选择字段类型">
							<el-option
						      v-for="item in fieldTypeData"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						    </el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="字段值" prop="fieldValue">
						<el-input v-model="dataruleCondition.fieldValue" clearable placeholder="字段值"></el-input>
					</el-form-item>
					<el-form-item label="部门" prop="deptVal">
						<comboDepartTree class="short-form" :position="false" @changes="getSelectDepart"></comboDepartTree>
					</el-form-item>
					<el-form-item label="条件排序" prop="conOrder">
						<el-input v-model="dataruleCondition.conOrder" placeholder="条件排序"></el-input>
					</el-form-item>
					<el-form-item label="登录人上级部门级别" prop="deptLevel">
						<el-input v-model="dataruleCondition.deptLevel" placeholder="登录人上级部门级别"></el-input>
					</el-form-item>
					<el-form-item label="状态" prop="status">
						<el-select v-model="dataruleCondition.status" placeholder="请选择状态">
							<el-option
						      v-for="item in statusOptions"
						      :key="item.value"
						      :label="item.label"
						      :value="item.value">
						      </el-option>
						</el-select>
					</el-form-item>
					<el-form-item style="text-align: right;width: 100%;">
						<el-button type="primary" @click.native="saveCondition()">确定</el-button>
						<el-button @click.native="dataruleConditionShow=false">取消</el-button>
					</el-form-item>
				</el-form>
			</el-dialog>
		</el-row>
	</section>
</template>
<script>
	import NProgress from 'nprogress'
	import comboDepartTree from 'components/comboDepartTree'

	import { getDataRuleList, getDataruleData, saveDatarule, delDatarule, getDataruleGroupList, getDataconditionList,getGroupListData
	,delDataruleCondition,saveDataruleCondition,getDataruleConditionData,delDataruleGroup,saveDataruleGroup,getDataruleGroupData} from '../../api/auth';
	export default {
		components: {
			comboDepartTree
		},
		data() {
			return {
				dataruleConditionShow: false,
				dataruleGroupShow: false,
				dataruleShow: false,
				ids:[],
				ruleId: '',
				groupId: '',
				dataruleConditiontable: {
					tableLoading: false,
					tableData: [],
					page: 1,
					rows: 10,
					total: 0,
					currentRow: null
				},
				dataruleGrouptable: {
					groupList:[],
					tableLoading: false,
					tableData: [],
					page: 1,
					rows: 10,
					total: 0,
					currentRow: null
				},
				dataruletable: {
					tableLoading: false,
					tableData: [],
					page: 1,
					rows: 10,
					total: 0,
					currentRow: null
				},
				dataruleCondition: {
					show: false

				},
				dataruleGroup: {

				},
				datarule: {
				},
				sqlOp: [{
		          value: 'lt',label: '小于'
		        }, {
		          value: 'equal',label: '等于'
		        }, {
		          value: 'gt',label: '大于'
		        }, {
		          value: 'lt=',label: '小于或等于'
		        }, {
		          value: 'gt=',label: '大于或等于'
		        }, {
		          value: 'likestart',label: '以..开始'
		        }, {
		          value: 'likeend',label: '以..结束'
		        }, {
		          value: 'like',label: '相似'
		        }, {
		          value: 'include',label: '包括在...'
		        }, {
		          value: 'noinclude',label: '不包括...'
		        }, {
		          value: 'noequal',label: '不等于'
		        }],
		        fieldTypeData: [{
		          value: 'string',label: '字符串类型'
		        }, {
		          value: 'boolean',label: '布尔类型'
		        }, {
		          value: 'cascadedept',label: '级联部门'
		        }, {
		          value: 'dept',label: '指定部门-不级联'
		        }, {
		          value: 'uservar',label: '登录用户'
		        }, {
		          value: 'busdept',label: '登录人所在部门(指定级别)'
		        }, {
		          value: 'subdept',label: '登录人所在班组'
		        }, {
		          value: 'alldept',label: '所有部门'
		        }, {
		          value: 'varDeptCasca',label: '角色部门级联用户部门级联'
		        }, {
		          value: 'varPartDeptCasca',label: '登录人变量级联部门'
		        }, {
		          value: 'userRoleDept',label: '登录人角色部门'
		        }],
		        conOp: [{
		          value: 'or',label: '或者'
		        }, {
		          value: 'and',label: '并且'
		        }],
		        statusOptions: [{
		          value: '1',label: '启用'
		        }, {
		          value: '0',label: '禁用'
		        }]
			}
		},
		methods: {
			gV(val){
				console.log(val)
			},
			getSelectDepart(id){
				this.dataruleCondition.deptVal = id;
			},
			getDataRuleList() {
				var para = {
					page:this.dataruletable.page,
					rows:this.dataruletable.rows
				};
				getDataRuleList(para).then((res) => {
					//this.operationcodes=res.obj.operationList.map(item=>item.operationCode);
					this.dataruletable.tableData = res.obj;
					this.dataruletable.total = res.total;
				});
			},
			getDataruleGroupList() {
				var para = {
					ruleId: this.ruleId,
					page:this.dataruleGrouptable.page,
					rows:this.dataruleGrouptable.rows
				};
				getDataruleGroupList(para).then((res) => {
					//this.operationcodes=res.obj.operationList.map(item=>item.operationCode);
					// this.checkedId=res.obj.operationcodes.map(item=>item.operationCode);
					// console.log(this.checkedId);
					this.dataruleGrouptable.tableData = res.obj;
					this.dataruleGrouptable.total = res.total;
					this.dataruleConditiontable.tableData = []
					this.dataruleConditiontable.total = 0;
				});
			},
			getDataconditionList() {
				var para = {
					groupId: this.groupId,
					ruleId: this.ruleId,
					page:this.dataruleConditiontable.page,
					rows:this.dataruleConditiontable.rows
				};
				getDataconditionList(para).then((res) => {
					//this.operationcodes=res.obj.operationList.map(item=>item.operationCode);
					// this.checkedId=res.obj.operationcodes.map(item=>item.operationCode);
					// console.log(this.checkedId);
					this.dataruleConditiontable.tableData = res.obj;
					this.dataruleConditiontable.total = res.total;
				});
			},
			//      handleNodeClick(data,node){
			//        this.dataId=data.id;
			//        this.getoperationList();
			//      },

			//规则条数改变回调
			handleSizeRuleChange(size) {
				this.dataruletable.rows = size;
				this.getDataRuleList();
			},
			//规则页码改变回调
			handleCurrentRuleChange(page) {
				this.dataruletable.page = page;
				this.getDataRuleList();
			},
			//分组条数改变回调
			handleSizeGroupChange(size) {
				this.dataruleGrouptable.rows = size;
				this.getDataruleGroupList();
			},
			//分组页码改变回调
			handleCurrentGroupChange(page) {
				this.dataruleGrouptable.page = page;
				this.getDataruleGroupList();
			},
			//条件条数改变回调
			handleSizeConditionChange(size) {
				this.dataconditiontable.rows = size;
				this.getDataconditionList();
			},
			//条件页码改变回调
			handleCurrentConditionChange(page) {
					this.dataconditiontable.page = page;
					this.getDataconditionList();
				},

			//列表选中事件
		handleConRowChange(val) {
			if(val){
				this.dataruleConditiontable.currentRow = val;
				//this.getDataruleGroupList();
			}
			//this.dataconditiontable.currentRow = val;
			//this.getMenuTree(this.table.currentRow.id);
			//this.ruleId = '';
			//this.getDataruleGroupList();
		},
		handleRuleSelectChange(val){
			if(val){
				this.ids = val;
			}
		},
		handleRuleRowChange(val) {
			if(val){
				this.dataruletable.currentRow = val;
				this.ruleId = val.id;
				this.getDataruleGroupList();
			}

		},
		handleGroupRowChange(val) {
			if(val){
				this.dataruleGrouptable.currentRow = val;
				this.groupId = val.id;
				this.getDataconditionList();
			}
		},
		//弹窗的列表回调
		//			handleSelectionChange1(val){
		//				this.dialog.leftSelected=val;
		//			},
		//			handleSelectionChange2(val){
		//				this.dialog.rightSelected=val;
		//			},
		addRule(){
  				this.dataruleShow=true;
  				this.datarule={};
  				this.datarule.status='1';
  			},
  		editRule(){
  			let ruleId=this.dataruletable.currentRow.id;
  				getDataruleData({id:ruleId}).then((res) => {
					this.datarule =res.obj;
					this.datarule.sql=res.attributes.sql;
					this.dataruleShow=true;
				});
  			},
		//删除规则
		delRule() {
			//let ruleId=this.dataruletable.currentRow.id;
			let rules=this.ids;
			this.$confirm('此操作将删除选中的规则, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let str='';
				for(let i=0;i<rules.length;i++){
					str+=',';
  					str+=rules[i].id;
  				}
				if(str.startsWith(","))
				str=str.substr(1);
				let para = {
					'ids': str
				};
				delDatarule(para).then((res) => {
					if(res.success == true) {
						this.$message({
							type: 'success',
							message: '删除成功'
						});
						this.getDataRuleList();
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消操作'
				});
			});
		},
		//提交
		saveRule() {
				let para =this.datarule;
				saveDatarule(para).then((res) => {
					if(res.success == true) {
						this.dataruleShow = false;
						this.getDataRuleList();
					}
				});
			},
		///////////////////////////////////////////////////////////////////////
		addGroup(){
  				this.dataruleGroupShow=true;
  				this.dataruleGroup={ruleId:this.ruleId,operate:'and'};
  				this.dataruleGroup.status='1';
  			},
  		editGroup(){
  			let groupId=this.dataruleGrouptable.currentRow.id;
  				getDataruleGroupData({id:groupId}).then((res) => {
					this.dataruleGroup =res.obj;
					this.dataruleGrouptable.groupList=res.attributes.datagrouprules;
					this.dataruleGroupShow=true;
				});
  			},
		//删除规则
		delGroup() {
			let groupId=this.dataruleGrouptable.currentRow.id;
			this.$confirm('此操作将删除选中的分组, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let para = {
					'id': groupId
				};
				delDataruleGroup(para).then((res) => {
					if(res.success == true) {
						this.$message({
							type: 'success',
							message: '删除成功'
						});
						this.getDataRuleGroupList();
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消操作'
				});
			});
		},
		saveGroup() {
				let para =this.dataruleGroup;
				saveDataruleGroup(para).then((res) => {
					if(res.success == true) {
						this.dataruleGroupShow = false;
						this.getDataruleGroupList();
					}
				});
	   },
		/////////////////////////////////////////
		addCondition(){
  				this.dataruleConditionShow=true;
  				this.dataruleCondition={
  					fieldName:'',
					conOperate:'',
					fieldType:'',
  					operate:'',
  					fieldValue:'',
  					deptLevel:1,
  					ruleId:this.ruleId,
  					groupId:this.groupId,
  					status:'1'
  				};
  			},
  		editCondition(){
  			let id=this.dataruleConditiontable.currentRow.id;
  				getDataruleConditionData({id:id}).then((res) => {
  					console.log(res.obj)
  					this.dataruleCondition=res.obj
//					this.$set(this,'dataruleCondition',res.obj)

  					this.$nextTick(()=>{
  						//this.$set(this.dataruleCondition,'sqlconditionoperate',res.obj.conOperate)
  						//console.log(this.dataruleCondition.conOperate)
  						this.dataruleConditionShow=true;
  					})

				});
  			},
		//删除规则
		delCondition() {
			let groupId=this.dataruleConditiontable.currentRow.id;
			this.$confirm('此操作将删除选中的条件, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let para = {
					'id': groupId
				};
				delDataruleCondition(para).then((res) => {
					if(res.success == true) {
						this.$message({
							type: 'success',
							message: '删除成功'
						});
						this.getDataconditionList();
					}
				});
			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消操作'
				});
			});
		},
		saveCondition() {
				let para =this.dataruleCondition;
				saveDataruleCondition(para).then((res) => {
					if(res.success == true) {
						this.dataruleConditionShow = false;
						this.getDataconditionList();
					}
				});
	   },
		//弹窗翻页回调
//		leftPageChange(val) {
//			this.dialog.leftPage = val;
//			this.getNoRoleList();
//		},
//		rightPageChange(val) {
//			this.dialog.rightPage = val;
//			this.getInRoleList();
//		}

	},
	computed: {
//			ruleId: function() {
//				if(this.dataruletable.currentRow) {
//					return this.dataruletable.currentRow.id;
//				}
//			},
//			groupId: function() {
//				if(this.dataruleGrouptable.currentRow) {
//					return this.dataruleGrouptable.currentRow.id;
//				}
//			}
		},
		mounted() {
			this.getDataRuleList();
			//this.getMenuTree('8afc99b04c26ad0b014c26af28900042');//默认获取管理员的权限设置
		}
	}
</script>
